
export default {
  namespace: 'global',

  state: {
    collapsed: false,
    notices: [],
    noticeCount: 0,
    currMenu: {},
  },

  effects: {
    *fetchNotices(_, { call, put }) {
      // const response = yield call(queryNotices, { pageSize: 8, current: 1, sortField: 'noticeTime', sortOrder: 'desc', });
      const response = {};
      if (response && response.code == 200) {
        // const data = response.data;
        let data = [];

        for (let n of response.data.records) {
          data.push({
            // avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
            title: n.notice.title,
            // description: n.notice.content,
            type: '通知',
            id: n.id,
            noticeTime: n.noticeTime,
            description: n.noticeTime,
          });
        }

        yield put({
          type: 'saveNotices',
          payload: { notices: data, noticeCount: response.data.totalElements },
        });
        yield put({
          type: 'user/changeNotifyCount',
          payload: response.data.totalElements,
        });
      }
    },
    
    // *fetchWithdrawCount(_, { call, put }) {
    //   const response = yield call(queryNotices, { pageSize: 8, current: 1, sortField: 'noticeTime', sortOrder: 'desc', });
    //   const response = {};
    //   if (response && response.code == 200) {
    //     // const data = response.data;
    //     let data = [];

    //     for (let n of response.data.records) {
    //       data.push({
    //         // avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ThXAXghbEsBCCSDihZxY.png',
    //         title: n.notice.title,
    //         // description: n.notice.content,
    //         type: '通知',
    //         id: n.id,
    //         noticeTime: n.noticeTime,
    //         description: n.noticeTime,
    //       });
    //     }

    //     yield put({
    //       type: 'saveNotices',
    //       payload: { notices: data, noticeCount: response.data.totalElements },
    //     });
    //     yield put({
    //       type: 'user/changeNotifyCount',
    //       payload: response.data.totalElements,
    //     });
    //   }
    // },
    
    *clearNotices({ payload }, { put, select }) {
      yield put({
        type: 'saveClearedNotices',
        payload,
      });
      const count = yield select(state => state.global.notices.length);
      yield put({
        type: 'user/changeNotifyCount',
        payload: count,
      });
    },
  },

  reducers: {
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    changeCurrMenu(state, { payload }) {
      return {
        ...state,
        currMenu: payload,
      }
    },
    saveNotices(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    saveClearedNotices(state, { payload }) {
      return {
        ...state,
        notices: state.notices.filter(item => item.type !== payload),
      };
    },
  },

  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      return history.listen(({ pathname, search }) => {
        if (typeof window.ga !== 'undefined') {
          window.ga('send', 'pageview', pathname + search);
        }
      });
    },
  },
};
